package com.example.testscrcap;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.Menu;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.kt.hwan.MScreenCap;

public class MainActivity extends Activity implements RadioGroup.OnCheckedChangeListener {
	
	private final static String tag = "cap";
	
	private MyApp mApp;
	
	private final static int MSG_CAPTURE=700;
	private final static int cap_time=100;
	
	public static int MirrorWidth;
	public static int MirrorHeight;
	public static int Transport; // 0 tcp, 1 udp
	
	public static MScreenCap screenCap;
	private Button btnStart, btnMirror, btnSend, btnSendStop;
	private EditText edRemoteIp;
	private RadioGroup rgRgb, rgRes, rgTransport;
	private EditText edWidth, edHeight;
	
	public String remoteIp;
	private SurfaceView sv;
	public Handler mHandler;
	private SharedPreferences preferences;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        mApp = (MyApp)getApplication();
        
        preferences = PreferenceManager.getDefaultSharedPreferences (this );
        String ipaddr = preferences.getString("addr" , ""); // addr 이름의 문자열 값을 가져 온다.
        
        MirrorWidth = 720;
        MirrorHeight = 1280;
        
        mHandler = new Handler() {

			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				super.handleMessage(msg);
				
				if(msg.what == MSG_CAPTURE) {
					Log.d(tag, "== msg_cap java...");
					screenCap.nvCaptureTx();
					mHandler.sendEmptyMessageDelayed(MSG_CAPTURE, cap_time);
				}
			}};
        
        initViews();
        
        edRemoteIp.setText(ipaddr);
        
        screenCap = new MScreenCap();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
    private void initViews() {
    	/*
    	btnStart = (Button)findViewById(R.id.btnStart);
    	btnStart.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				screenCap.nvstart();
			}
		});
    	*/
    	btnSend = (Button)findViewById(R.id.btnSend);
    	btnSend.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				remoteIp = edRemoteIp.getText().toString();
				Log.d(tag, "send remote = "+remoteIp);
				screenCap.nvSetMirrorCfg(MirrorWidth, MirrorHeight);
				screenCap.nvStartSendFrame(remoteIp, 9009, Transport);
				//mHandler.sendEmptyMessageDelayed(MSG_CAPTURE, cap_time);
			}
		});
    	
    	btnSendStop = (Button)findViewById(R.id.btnSendStop);
    	btnSendStop.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				mHandler.removeMessages(MSG_CAPTURE);
				screenCap.nvStopSendFrame();
			}
		});
    	
    	
    	btnMirror = (Button)findViewById(R.id.btnMrror);
    	btnMirror.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
				Log.d(tag, "mirror clicked...");
				Intent intent = new Intent(MainActivity.this, MirrorSurface.class);
				remoteIp = edRemoteIp.getText().toString();
				intent.putExtra("ip", remoteIp);
				intent.putExtra("port", 9009);
				startActivity(intent);
				/*
				sv = new SurfaceView(MainActivity.this);
				
				sv.getHolder().addCallback(new SurfaceHolder.Callback() {
					
					@Override
					public void surfaceDestroyed(SurfaceHolder arg0) {
						// TODO Auto-generated method stub
						
					}
					
					@Override
					public void surfaceCreated(SurfaceHolder arg0) {
						// TODO Auto-generated method stub
						screenCap.nvdraw(arg0.getSurface());
					}
					
					@Override
					public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
						// TODO Auto-generated method stub
						
					}
				});*/
				//screenCap.nvdraw(surface);
				 
				
			}
		});
    	
    	
    	edRemoteIp = (EditText)findViewById(R.id.edRemoteIp);
    	
    	rgRgb = (RadioGroup)findViewById(R.id.rgRgb);
    	rgRes = (RadioGroup)findViewById(R.id.rgRes);
    	rgTransport = (RadioGroup)findViewById(R.id.rgTransport);
    	
    	rgRgb.setOnCheckedChangeListener(this);
    	rgRes.setOnCheckedChangeListener(this);
    	rgTransport.setOnCheckedChangeListener(this);
    	
    	
		checkResSelection();
		checkTransportSelection();
		
		edWidth = (EditText)findViewById(R.id.edWidth);
		edHeight = (EditText)findViewById(R.id.edHeight);
			
    }

    private void checkTransportSelection() {
    	int id = rgTransport.getCheckedRadioButtonId();
		if(id == R.id.rdTcp)
			Transport = 0;
		else if(id == R.id.rdUdp)
			Transport = 1;
    }
    
    private void checkResSelection() {
    	int id;
    	id = rgRes.getCheckedRadioButtonId();
		if(id == R.id.rd720) {
			//Log.d(tag, "720p selected....");
			MirrorWidth = 720;
			MirrorHeight = 1280;
		}
		else if(id == R.id.rd480) {
			//Log.d(tag, "480p selected...000.");
			MirrorWidth = 480;
			MirrorHeight = 640;
		}
		else if(id == R.id.rdUserRes)
		{
			String sw, sh;
			sw = edWidth.getText().toString();
			sh = edHeight.getText().toString();
			if(sw != "")
				MirrorWidth = Integer.parseInt(sw);
			else
				MirrorWidth = 720;
			
			if(sh != "")
				MirrorHeight = Integer.parseInt(sh);
			else
				MirrorHeight = 1280;
			
		}
		
		Log.d(tag, String.format("set mirror dimension w=%d,  h=%d", MirrorWidth, MirrorHeight));
    }

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		Log.d(tag, "onStop....");
		
		Editor edit = preferences.edit();
		String txt = edRemoteIp.getText().toString();
		edit.putString( "addr", txt);
		edit.commit();

	}


	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		Log.d(tag, "OnDestroy......");
		screenCap.nvStopSendFrame();
		screenCap.nvStopRecvFrame();
		screenCap = null;
	}


	@Override
	public void onCheckedChanged(RadioGroup arg0, int arg1) {
		// TODO Auto-generated method stub
		if(arg0 == rgRgb) {
			
		}
		else if(arg0 == rgRes) {
			checkResSelection();
		}
		else if(arg0 == rgTransport)
		{
			checkTransportSelection();	
		}
		
	}
    
}
